#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>


void tweakedScan(double *X, int n) {
	int i, logn, pow_d, pow_d1, d, tmp, limit;
	//TYPE *c = (TYPE *)X;

	logn = (int)floor(log2(n));

	//double t1 = omp_get_wtime();

	for (d = 0; d < logn; d++) {
		pow_d = 1 << d;
		pow_d1 = pow_d << 1;
		limit = n - pow_d1;

#pragma omp parallel for shared(X) schedule(guided) 
		for (i = -1; i < limit; i += pow_d1) {
			X[i + pow_d1] += X[i + pow_d];
		}
	}

	for (d = d; d >= 0; d--) {
		pow_d = 1 << d;
		pow_d1 = pow_d << 1;
		limit = n - pow_d;

#pragma omp parallel for shared(X) schedule(guided) 
		for (i = pow_d1-1; i < limit; i += pow_d1) {
			X[i + pow_d] += X[i];
		}
	}
	//double t2 = omp_get_wtime();

	//printf("\n\t inside scan : time = %g", (t2-t1));

}
